function nas
clc

doKeyboard=false;

ListenChar(2);
KbName('UnifyKeyNames');
uKey=KbName('UpArrow');
dKey=KbName('DownArrow');
qKey=KbName('q');
lKey=KbName('l');
fKey=KbName('f');

Priority(1);
win=Screen('openwindow',max(Screen('screens')));

try
    [y,fs] = aiffread('nas.aiff');
    y=double(y);
    
    s=120000;
    e=619000;
    
    clips{1}={[140000 160000]-s,'nigga'};
    clips{2}={[248500 288500]-s,'bling'};
    
    y=y(s:e,:);
    y=y/max(abs(y(:)));
    
    sz=[];
    [sz(1) sz(2)]=Screen('WindowSize', win);
    
    biggest=0;
    for i=1:length(clips)
        g=zeros(fliplr(sz));
        inds=clips{i}{1}(1):clips{i}{1}(2);
        v=interp1(inds,y(inds,1),linspace(inds(1),inds(end),sz(1)));
        v=v-min(v);
        v=v/max(v);
        v=ceil(sz(2)/2 + v*sz(2)/5);
        v(v==0)=1;
        g(sub2ind(size(g),v,1:sz(1)))=1;
        t(i)=Screen('maketexture',win,g,[],[],1);
        if length(inds)>biggest
            biggest=length(inds);
        end
    end
    
    i=1;
    draw=true;
    nextDrawOK=false;
    
    buffsize=2500;%1500; %6000
    
    PsychPortAudio('Verbosity',1);
    
    a = PsychPortAudio('Open',[],[],[],fs,2);
    PsychPortAudio('RunMode', a,1);
    PsychPortAudio('FillBuffer', a, y(1:buffsize,:)');
    
    next=buffsize+1;
    lastX=[];
    done=false;
    
    PsychPortAudio('Start', a, 0);
    
    while ~done
        nnext=next+buffsize-1;
        
        [sx sy buttons]=GetMouse(0);
        [keyIsDown, secs, keyCode] = KbCheck;
        if buttons(1) && ~isempty(lastX)
            data=interp1(y,clips{i}{1}(1)+(clips{i}{1}(2)-clips{i}{1}(1))*linspace(lastX,sx,buffsize)/sz(1),'linear')';
        elseif keyCode(lKey)
            chunkStart=clips{i}{1}(1);
            while chunkStart<clips{i}{1}(2)
                chunkEnd=min(chunkStart+buffsize-1,clips{i}{1}(2));
                data=y(chunkStart:chunkEnd,:)';
                if chunkEnd<clips{i}{1}(2) && keyCode(lKey)
                    next=nnext+1;
                    nnext=next+buffsize-1;
                    u=PsychPortAudio('FillBuffer', a, data, 1);
                    [keyIsDown, secs, keyCode] = KbCheck;
                else
                    break
                end
                chunkStart=chunkEnd+1;
            end
        elseif keyCode(fKey)
        else
            data=y(mod(linspace(next,nnext,buffsize)-1,size(y,1))+1,:)';
        end
        lastX=sx;
        
        u=PsychPortAudio('FillBuffer', a, data, 1);
        
        if false && u~=0
            warning('uflow')
            break
        end
        next=nnext+1;
        
        if keyIsDown
            if nextDrawOK
                if keyCode(uKey)
                    i=i+1;
                    draw=true;
                elseif keyCode(dKey)
                    i=i-1;
                    draw=true;
                elseif keyCode(qKey)
                    done=true;
                end
            end
        else
            nextDrawOK=true;
        end
        
        if  draw
            i=mod(i-1,length(clips))+1;
            Screen('drawtexture',win,t(i));
            Screen('drawtext',win,clips{i}{2},0,0,255);
            Screen('flip',win);
            nextDrawOK=false;
        end
        draw=false;
    end
    
catch ex
    getReport(ex)
    doKeyboard=true;
end
ListenChar(0);
sca
Priority(0);
PsychPortAudio('Stop', a);
PsychPortAudio('Close');
if doKeyboard
    keyboard
end
end